<template>
  <div>
    <div>
      <button @click="addSegment">添加</button>
      <input type="number" v-model="segmentCount" @input="updateSegments" />
      <button @click="updateSegments">份数</button>
    </div>
    <br />
    <br />
    <br />
    <div class="ray">
      <div
        class="ray-segment"
        v-for="(segment, index) in segments"
        :key="index"
        :style="{ width: segment.width + '%', left: segment.left + '%' }"
      ></div>
      <div
        class="ray-tick"
        v-for="(tick, index) in ticks"
        :key="index"
        :style="{ left: tick.left + '%' }"
      ></div>
      <div
        class="ray-label"
        v-for="(tick, index) in ticks"
        :key="index"
        :style="{ left: tick.left + '%' }"
      >
        {{ tick.label }}
      </div>
      <div class="ray-arrow" :style="{ left: arrowPosition + '%' }"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rayWidth: 400,
      segmentCount: 1,
      segments: [{ width: 100, left: 0 }],
      ticks: [],
      arrowPosition: 0,
    };
  },
  methods: {
    addSegment() {
      this.segments.push({ width: 0, left: 100 });
      this.updateTicks();
    },
    updateSegments() {
      const segmentWidth = 100 / this.segmentCount;
      this.segments.forEach((segment, index) => {
        segment.width = segmentWidth;
        segment.left = segmentWidth * index;
      });
      this.updateTicks();
    },
    updateTicks() {
      const tickWidth = 100 / (this.segmentCount + 1);
      this.ticks = [];
      for (let i = 0; i <= this.segmentCount; i++) {
        if (i === 0) {
          this.ticks.push({ label: "0", left: 0 });
        } else if (i === this.segmentCount) {
          this.ticks.push({ label: "1", left: 100 });
        } else {
          this.ticks.push({
            label: `${i}/${this.segmentCount}`,
            left: tickWidth * i,
          });
        }
      }
    },
  },
};
</script>

<style>
.ray {
  height: 10px;
  background-color: #ccc;
  position: relative;
}

.ray-segment {
  height: 100%;
  background-color: #f00;
  position: absolute;
  top: 0;
}

.ray-tick {
  width: 1px;
  height: 10px;
  background-color: #000;
  position: absolute;
}

.ray-label {
  position: absolute;
  bottom: -30px;
  transform: translateX(-50%);
}

.ray-arrow {
  width: 0;
  height: 0;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 10px solid #000;
  position: absolute;
  top: -5px;
  transform: translateX(-50%);
}
</style>
